

import java.io.*;
import java.net.*;

public class Client 
{
	
	
	
	public Thread threadRecieve;
	public Thread threadSend;
	
	
	public static void RececieveData(Socket ss)
	{
		try	{				
			ReciveServer sserver = new ReciveServer(ss);					
		}catch(Exception e)		{
			System.out.println("There're some error");
		}	
		
	}
	
	
	public static void main(String arg[])
	{
		try
		{
			System.out.println("main");
			Socket ss = new Socket("localhost",3211);
			System.out.println(ss.getPort());
			
			RececieveData(ss);// gọi thread để lúc nào cũng nhận data từ server
			
			while(true)
			{
				PrintWriter out = new PrintWriter(ss.getOutputStream(),true); 
				
				Thread.sleep(1000);
				out.println("Data Send From Client.");
				
			}
		
			
		}
		catch(Exception e)
		{
			System.out.println("There're some error");
		}
	}
}



class ReciveServer implements Runnable { 
	
	private Socket theclient; 
	ServerSocket serverSocket = null; 
	
	public ReciveServer (Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} 
	
	public void run() { 
		try 
		{ 
			//PrintWriter out = new PrintWriter(theclient.getOutputStream(),true); 
			BufferedReader in = new BufferedReader(new InputStreamReader(theclient.getInputStream())); 
			
			String inputLine; 
			while((inputLine=in.readLine()) != null) { 
				
				System.out.println("From server:"+inputLine);	
				
				if (inputLine.equals("Bye")) 
					break; 				
				
			

			} 
		//out.flush(); 		
		//out.close(); 
		in.close(); 
		theclient.close(); 
		serverSocket.close(); 
		
		} 
		catch (IOException e) { 
			e.printStackTrace(); 
		} 
	} 
	
}

